/**
 * 基于async-validator 输入有效校验
 * @author xiufu.wang
 */
import Schema from 'mars-pro-async-validator';
import { format, isEmptyValue } from 'mars-pro-async-validator/lib/util';
import validators from 'mars-pro-async-validator/lib/validator/index';
import resolveDyncDate, {moment} from './resolve-dyncdate'
import { messages } from "./async-validator-form-messages";

const EMAIL_REGEX = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
const NUMBER_REGEX = /^-?[0-9]*.?[0-9]+$/
const MOBILE_REGEX = /^1[0-9]\d{9}$/
const PHONE_REGEXX = /^(([0-9]{3,4})-)?[0-9]{7,8}$/
const IDCARD_REGEX = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(?:[0-9]|X)$|^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/
const CHINESE_REGEX = /^[\u4e00-\u9fa5]+$/
const UNCOMMAFY_REGEX = new RegExp("[^0-9-.]", ["g"])
const COMMAFY_REGEX = /(\d{3})(?=\d)/g


function toNormalPromise(prm, input) {
    prm.messages(messages)
    return new Promise((resolve) => {
        prm.validate({ input }, (errs) => {
            if (errs) {
                resolve({
                    err: (errs ? errs[0].message : null)
                })
            } else {
                resolve({
                    ok: true
                })
            }
        })
    })
}

//字符串
export function string_valid({ max, min, input, message }) {
    return toNormalPromise(new Schema({
        input: {
            fullField: ' ',
            whitespace: true,
            type: 'string',
            min,
            max,
            message,
        }
    }), input)
}

// 邮箱
export function email_valid({input, message }) {
    return toNormalPromise(new Schema({
        input: {
            fullField: ' ',
            whitespace: true,
            type: 'email',
            message
        }
    }), input)
}

//数字
export function number_valid({ max, min, input, message }) {
    let schema = null
    return toNormalPromise(schema = new Schema({
        input: {
            fullField: ' ',
            whitespace: true,
            type: 'number',
            max,
            min,
            transform(d) {
                if (isEmptyValue(d, 'string')) {
                    return null
                }
                return +d
            },
            validator: (...args) => {
                const v = args[1]
                const rule = args[0]
                if (!isEmptyValue(v) && (isNaN(v) && !NUMBER_REGEX.test(v))) {
                    return Promise.reject(format(message || schema._messages.types.number, input, 'number'))
                } else {
                    validators[schema.getType(rule)](...args)
                }
            }
        }
    }), input)
}

//电话号码
export function phone_valid({ input, message }) {
    let schema = null
    return toNormalPromise(schema = new Schema({
        input: {
            fullField: ' ',
            whitespace: true,
            type: 'string',
            validator: (...args) => {
                const v = args[1]
                const rule = args[0]
                if (!isEmptyValue(v, 'string') && !MOBILE_REGEX.test(v) && !PHONE_REGEXX.test(v)) {
                    return Promise.reject(format(message || '联系号码格式错误', input, 'phone'))
                } else {
                    validators[schema.getType(rule)](...args)
                }
            }
        }
    }), input)
}

//正则
export function pattern_valid({ min, max, input, message, pattern }) {
    let schema = null
    return toNormalPromise(schema = new Schema({
        input: [{
            fullField: ' ',
            type: 'pattern',
            pattern,
            message
        }, {
            type: 'string',
            fullField: ' ',
            min,
            max,
            message
        }]
    }), input)
}

export function date_valid({ input, min, max, message }) {
    return toNormalPromise(new Schema({
        input: {
            fullField: ' ',
            type: 'date',
            max,
            min,
            transform(d) {
                if (isEmptyValue(d, 'string')) {
                    return null
                }
                let res = moment(d)
                if (!res.isValid()) { 
                    return null
                }
                return res._d
            }
        }
    }), input)
}